// subpage/pages/carbeand/carbeand.js
import service from '../../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    carnumner: '',
    errorMsg: '',
    oid : ''
   

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    console.log('options',options)
    if(options.carnumber) {
      this.setData({
        carnumner: options.carnumber,
        oid: options.oid
      })
      this.selectComponent("#car").setCar(that.data.carnumner)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(!this.data.carnumner){
      this.getUser()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    // clearTimeout
    // this.getOpenerEventChannel().emit('onCar',this.data.carnumner);
	  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onChange(event) {
    // event.detail 为当前输入的值
    console.log(event.detail);
  },
  //修改车牌号
  editNumber (e){
    console.log('dddd',e)
    let car =  e.detail
    // return
    // let car = this.data.carnumner.toLocaleUpperCase()
    var xxreg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DABCEFGHJK]$)|([DABCEFGHJK][A-HJ-NP-Z0-9][0-9]{4}$))/  // 2021年新能源车牌不止有DF
    var creg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/
    if(car.length == 7){
      if(!creg.test(car)){
        wx.showToast({
          title: "车牌号格式不正确",
          icon: "none"
        })
        return
      }
    } else if(car.length == 8){
      if(!xxreg.test(car)){
        wx.showToast({
          title: "车牌号格式不正确",
          icon: "none"
        })
        return
      } 
    } else{
      wx.showToast({
        title: "请输入正确的车牌",
        icon: "none"
      })
      return
    }
    let that = this
    var url,data = {}
    if(that.data.oid){
      url = '/parking/update'
      data = {plate: car, oid: that.data.oid}
    }else{
      url = '/user/update'
      data = {plate: car}
    }
    let result = service('POST',url,data).then(res => {
      console.log('当前车牌号',car)
      // return
      if(res.data.code ==0){
        // wx.showToast({
        //   title: '修改成功',
        //   icon:'none'
        // })
        that.setData({
          carnumner: car
        })
        wx.showToast({
          title: '修改成功',
          icon: 'success',
          duration: 1500,
          success: function () {
            console.log('success')
            that.getOpenerEventChannel().emit('onCar',car);
            wx.navigateBack()
          }
         })
        // setTimeout(() => {
        //   this.getOpenerEventChannel().emit('onCar',car);
	      // 	wx.navigateBack();
        // }, 1000);
        
      }else{
        wx.showToast({
          title: '修改失败，请稍后重试',
          icon:'none'
        })
      }
    })
  },
    //个人信息
    getUser() {
      let that = this
      let resu =  service("GET", '/user/info', {}).then(res => {
        console.log('初始化个人信息', res)
        if (res.data.code == 0) {
          that.setData({
            carnumner:res.data.user.plate ? res.data.user.plate: ''
          })
          this.selectComponent("#car").setCar(that.data.carnumner)
        }else{
          if(res.data.code ==466){
            wx.showToast({
              title: '登录失效，请重新登录',
              icon:'none'
            })
            setTimeout(() => {
             wx.navigateTo({
               url: '/subpage/pages/login/login',
             })
            }, 500);
          }
        }
      })
    },
    carSet(e) {
      this.setData({
        carnumner:e.detail
      })
    }
})